package com.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;

import com.tg.appcommon.android.TGLog;

public class MoreClickView extends View {
    private final static int MAX_CLICK = 5;
    private  int clicked = MAX_CLICK;
    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            clicked = MAX_CLICK;
        }
    };
    private setOnMoreClickListener onMoreClickListener;

    public void setOnMoreClickListener(setOnMoreClickListener onMoreClickListener) {
        this.onMoreClickListener = onMoreClickListener;
    }

    public MoreClickView(Context context) {
        super(context);
    }

    public MoreClickView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MoreClickView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            handler.removeMessages(0);
            handler.sendEmptyMessageDelayed(0, 1000);
        }else if (event.getAction() == MotionEvent.ACTION_UP){
            clicked --;
            TGLog.d("onTouchEvent "+clicked);
            if (onMoreClickListener!=null){
                onMoreClickListener.onClick(clicked,clicked==0);
            }
        }

        return true;
    }
    public interface  setOnMoreClickListener{
        void  onClick(int num ,boolean enable);
    }
}
